home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7888 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Exceptions and operator new. Help
  4. Message-ID: <1996Feb16.174250.1777@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 16 Feb 96 17:42:50 WET
  7. References: <4fu14a$2vg@news.tamu.edu>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4fu14a$2vg@news.tamu.edu> tpradeep@cs.tamu.edu (Pradeep K  
  12. Tapadiya) writes:
  13.  
  14. > According to the C++ specs, a new will throw a xalloc exception
  15. > if memory allocation fails. 
  16.  
  17. According to the draft standard, it throws a bad_alloc exception.
  18.  
  19. > The return value from new need not be
  20. > NULL. In addition, the constructor can throw a different exception
  21. > as defined by you, the programmer. This means, each time, I do a
  22. > new, I have to do something like 
  23. >    myObject* p = NULL;
  24. >    try {
  25. >      myObject* p = new myObject;
  26. >    }
  27. >    catch (xlloc e) {
  28. >      // do not delete p here since it was never allocated
  29. >      ...
  30. >    }
  31. >    catch (myException& e) {
  32. >       delete p;
  33. >     ...
  34.  
  35. No. The memory allocated will be released when the constructor code throws
  36. an exception as well.
  37.  
  38. > Moreover, if myObject's destructor has to do some
  39. > memroy cleanup, for example,
  40. > myObject::~myObject ()
  41. > {
  42. >    delete [] m_pVarList;
  43. > }
  44. >
  45. > then, the constructor will probably has to do something like
  46. > myObject::myObject ()
  47. > {
  48. >    try {
  49. >      m_pVarList = new char [10];
  50. >    }
  51. >    catch (xalloc e) {
  52. >      m_pVarList = NULL; // explicitly set it to NULL as destructor will
  53. >                         // try to delete a bogus pointer
  54. >      throw xalloc ();
  55. >    }
  56. > }
  57.  
  58. You don't normally have to do this. Assuming your code looks like:
  59.  
  60.     void f()
  61.     {
  62.         myObject *p = new myObject;
  63.         // do things with *p
  64.         delete p;
  65.     }
  66.  
  67. Then, if an exception is thrown in allocating or constructing the new  
  68. object and f() doesn't attempt to catch it, the delete statement is never  
  69. reached. Instead, control will be passed directly to the latest try block  
  70. entered that wants to catch the exception thrown.
  71.  
  72. > Now, setnewHandler adds another variable to the model. As you cannot
  73. > rely on what third party library is doing, anytime you do a new on
  74. > your object, you will explictly have to do a setnewhandler to your
  75. > handler.
  76.  
  77. A quality library probably can be trusted to either install a reasonable  
  78. new_handler or better yet, only use its own new_handler for its own  
  79. dynamic allocation requests. 
  80.  
  81. > Though we have been programming in C++ for quite some time, this is
  82. > the first time our company is venturing out into the world of
  83. > exceptions. It now appears exceptions would make the development process
  84. > more complicated.
  85.  
  86. True, exception handling is controversial. But not for the reasons you  
  87. think it is. The real caveats are far more subtle. Exceptions are not an  
  88. easy subject, and you should probably make sure you understand the  
  89. exception mechanism a bit better before you start using them.
  90.  
  91. In the mean time, you can use the overloaded operator new that takes an  
  92. extra argument of type nothrow. This version will not throw a bad_alloc  
  93. exception on allocation failure, but return 0 instead. 
  94.  
  95. Good luck and best regards,
  96.  
  97. - Wil
  98.   
  99.